<template>
  <div>
    <div v-show="step == 1">
      <merge-step1 ref="step1" :conditions="conditions" @onNextStep="toStep2" @onPrevious="onPrevious"></merge-step1>
    </div>
    <div v-show="step == 2">
      <merge-step2 ref="step2" @onNextStep="toStep3" @onPrevious="onPrevious"></merge-step2>
    </div>
    <div v-show="step == 3">
      <merge-step3 ref="step3" @onNextStep="toStep4" @onPrevious="onPrevious"></merge-step3>
    </div>
    <div v-show="step == 4">
      <merge-step4 ref="views" @onNextStep="toSubmit" @onPrevious="onPrevious"></merge-step4>
    </div>
  </div>
</template>

<script>
import { saveMerge } from './saleDelivery'

import mergeStep1 from './mergeStep1.vue'
import mergeStep2 from './mergeStep2.vue'
import mergeStep3 from './mergeStep3.vue'
import mergeStep4 from './mergeStep4.vue'

export default {
  components: {
    mergeStep1,
    mergeStep2,
    mergeStep3,
    mergeStep4
  },
  data() {
    return {
      step: 1,
      conditions: [],
      submitData: {
        deliveryOrder: {},
        deliveryOrderDetails: [],
        invoices: []
      }
    }
  },
  computed: {},
  created() {
    if (this.$route.params.goodsId) {
      this.conditions = this.$route.params.goodsId
    } else {
      this.$router.push({
        name: 'undeliveredOrder',
        params: {
          isJump: false
        }
      })
    }
  },
  activated(){
    console.log('activated',this.$route);
  },
  mounted() {
    console.log(this.$route);
  },
  methods: {
    onPrevious() {
      this.step--
    },
    toStep2(val) {
      console.log(val)
      this.submitData.deliveryOrderDetails = val
      this.step = 2
      this.$refs.step3.setformDatas(val)
    },
    toStep3(val) {
      console.log(val)
      this.submitData.invoices = val
      this.step = 3
    },
    toStep4(val) {
      console.log(val)
      this.submitData.deliveryOrder = val
      console.log('预览', this.submitData)
      this.step = 4
      this.$refs.views.showView(this.submitData)
    },
    toSubmit() {
      console.log('提交', this.submitData)
      this.saveMerge(this.submitData)
    },
    // 保存信息
    async saveMerge(val) {
      console.log(val);
      let key = 'updatable'
      this.$message.loading({ content: '提交中',key })
      let res = await saveMerge(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1') {
        this.$message.success({ content: '提交成功',key, duration: 2 })
        this.$store.state.deliverOrder=this.submitData
        setTimeout(()=>{
          this.$router.push({
            name: 'undeliveredOrder',
            params: {
              isJump:false,
              pathName:this.$route.name,
            } 
          })
        },2000)
      } else {
        this.$message.success({ content: '提交失败,请稍后再试',key, duration: 2 })
      }
    }
  }
}
</script>

<style></style>
